So today we're going to have a look at keyframe frame.
Now this be split up into two sections.
Today we're having a look at just Keyframe itself.
And next lecture we're going to have a look at one of the applications of keyframe which is to do with
the camera.
Okay.
Now keyframe is something that you actually have seen before over in the properties section down here,
um, position and orientation are listed under um keyframe here.
Um, now, I don't really like having done this because, um, you only now are learning about what
keyframe means.
So it's it's not good for people learning if we're putting these together as keyframe, but as regardless
of point, we're having a look what keyframe is today.
So keyframe, fundamentally, it's just a more advanced method of changing an object's position or rotation.
Basically, they're combined into one and um, it works by storing both a position and rotation rather
than just one or the other.
And there's a whole range of different functions and stuff you can use to do much more advanced things
than the vector three functions.
Then the vector three function offers, for example, and lots of other applications, as well as when
you start.
When you stop looking at parts so much, um, objects start to use keyframe a lot more rather than position.
So when you do get into some coding, some more advanced objects keyframe will be much more a much more
preferred method, basically.
Okay, so I'm going to create a script in the workspace here, which is basically going to script a
script here.
We're just going to give you, um, all the all the parts of keyframe which make it so much more useful,
basically.
Okay.
So I'm going to just start by adding in one part and just showing you some code about one.
So local part is equal to game dot workspace dot part okay.
So firstly keyframe fundamentally you can use keyframe dot new just the same way you would use Vector
Freed on you.
So you can instead of saying part dot position you can reference part dot keyframe and you can make
this equal to see frame dot new, not vector not new, but keyframe dot new.
And this in here can have three numbers as a vector three.
So 123 just like that.
So it can work the exact same way as a vector three you can also add value.
So.
So part frame is equal to Patsy frame plus.
But we're going to add a vector three new here.
Okay 123.
Like this here.
You don't add.
You don't add another keyframe because objects only have one keyframe.
So you add a vector three there instead.
So just is this a show you really how you can use keyframes.
Just just normally like you would do with vector Freeze for example.
Um, some of the other stuff I'm going to show you here now though, um, is what makes it a bit more
useful.
So let's take this part here.
Over over here for example.
Okay.
Um, we can using a function of, of keyframe, rotate this part here to actually look at the spawn
location or any other object.
So to do this we can say part keyframe is equal to.
Instead of writing keyframe new we're going to add keyframe dot.
Look at.
And this has two keyframes.
One is its position.
And also sorry to vector threes.
One is its position and the second is the position of it's looking at.
Um so the position is it is at is just its current position.
So part dot position not going to move it.
We're just going to rotate it to look at a certain point.
And a point we're going to make it look at is the spawn location.
So game dot workspace dot spawn location dot position there as remember these are vector threes.
So you have to use position like that.
And if I quickly show you this it is pretty cool.
Um, if I quickly run the game.
Why is it taking so long?
It's taking a while to run a game.
This isn't good.
I haven't added any weird scripts that might have it do this.
Mm.
Applying edits and following.
Okay, let's try again.
That's strange.
Um.
Script?
No.
Only best script in my game.
Okay, let's try out one more time then.
Okay.
That's good.
Let's go.
My part has vanished.
Where's it gone?
Oh, it's now here.
So you can see it's moved up slightly because of the other two lines of code I wrote, but it's been
rotated looking at that object there, which is really cool.
So you can use position to influence its rotation by using that look at function.
This is kind of the thing we're going to look at today.
Really just some additional functions of keyframe that just things that would be very complicated or
even impossible to script using a Vector3 instead of keyframe.
Okay.
Um, now applications of look at we will explore a lot more next lesson.
But um, if you're wanting something which follows the like maybe some creepy eyes which are always
facing the player in like a horror game, for example.
That's an example where you can use look at for um, okay, now let's say you're wanting to use a rotation.
Well, we can say keyframe is equal to anything.
I'm going to make it equal to a new keyframe.
Keyframe dot new one two, three.
And to rotate it we can times by keyframe dot angles.
And this here, um, is basically, um, the equivalent of changing the rotation basically.
Now this is where the maths we looked at, um, right at the end of the last scripting section comes
into play.
As you will see, we have our X, R, y, and Z instead of just x, y and z.
What is our stand for?
Take a guess if you remember the math section.
Um, but it's radians.
Radians come into play here.
See frame does not use degrees.
It uses radians.
Um so if you want to, if you, unless you are very confident on radians, which you probably aren't,
um, you want to convert degrees into radians, which we use the math dot rad function for that one.
There.
It looks over this in the math in the math function section.
So have a look at that.
If you are a bit confused on this I'm just going to rotate it by 90 degrees.
And the other two I'm not going to rotate rotate it by anything.
So instead of writing math dot rad zero I'm just going to write zero, basically because um, zero degrees
is equal to zero radians.
They both mean the same thing.
But any other value you do want to change it.
Okay.
So that's how you can change rotation using this as well.
What I'm going to do is I'm just going to note out the rest of this here, just because it might mess
up the next bit.
I'm going to write.
Um, now there's two more parts really of frame that I want to show you today.
The first is the lerp um, function.
Now, lerp is a really, really cool function.
And it can be used for quite a lot of things as well.
Um, so that in two parts here.
There's going to be called, um.
Part one.
And this is here is going to be it's going to be called.
Part zero that.
Okay, so what lerp allows you to do is it allows you to position one object in directly in between
two other objects.
Okay.
Um, so to do this you will set the part keyframe or whatever, whatever keyframe to equal the first
part.
So in this case our part zero.
So I'm going to make a variable for this.
I'm only going to use it once.
So game the workspace dot part zero.
Dot frame.
I'm going to lerp that c frame.
So by writing colon lerp like this here.
Um now some of the explanation explanations and stuff are quite mathsy here as, as c frame is, is
very maths related.
So, um, don't get don't get put off by some of the weird explanations.
Um, so in here.
We write two things.
We write the goal and the alpha.
What these mean?
The goal is the second part that we're wanting to position it in between.
So it's positioned to here.
The part we're wanting to align this with is this second part up here.
We're wanting to put it some way in between these two objects.
So just reference that part game the workspace dot part one dot position I'm sorry not dot position.
Dot frame is asking the keyframe up here.
One of the things people get most confused by with keyframe is what's asking for keyframe value, and
what's asking for Vector3 value can take a bit of practice just to remember those, and afterwards we
write down an alpha as a number.
Doesn't really help the explanations here, but basically it's the ratio in between these two objects.
So an alpha of zero would position it exactly.
Just here an alpha of one will position it all the way up here 0.5 would be halfway, 0.25 would be
about here, 0.75 here.
And you get the point.
It's a ratio 0 to 1 on how far it is between these two objects.
So I'm going to put down 0.5.
But you can vary that however you'd like.
And let's try this out if we run the game.
Cool.
It's now positioned it exactly halfway through these two objects.
Now, this would absolutely be something you'd be able to do coding yourself without using the lerp
function, but quite a lot of calculations, quite a lot of maths involved here especially.
You have to you have to have to calculate magnitude and all kind of stuff.
It'd be quite complicated.
So it takes what would be quite complicated and makes it very simple.
Like this here.
Now an application for this, I think a massive one is to do with health bars and stuff like that.
So you can have one frame at the start and one frame at the end, and a bar between each one.
And you can use this to position the bar at different areas.
So position it halfway means it's somewhere like this 0.25.
It's over here and 0.75 it's over way all the way over here.
So you can you can change how much of the how big the bar is just by a single number here.
So that is a really good application of lerp.
And there are many other applications of this as well.
And it's just a general quite cool thing in general as well.
One last thing I want to show you here about keyframe before we move on is to do with the look vectors.
So these allow you to very quickly, um, move and move one of these objects based on where its faces
are well, facing.
So, um, you can right down here.
Patsy Frame is equal to the current keyframe plus or minus a look vector.
Um, now there are three types of look vectors.
There's the look vector, up vector and right vector.
This is basically moving it in the position that these faces are facing.
So the up the look vector um is the front face the one that's looking through basically.
So it'd be moving it in the direction that it's facing.
And the right vector with a direction for its right face is pointing towards and so on.
So to do this you would write down part dot see frame dot look vector like that there.
And if you want to extrude it by one unit.
But if you want to move it by more you can times it by I don't know, ten for example.
That's then moving it by ten units.
And the cool bit, the cool idea of using um, look vectors instead of just adding a vector three by
ten is because the vector three uses a global axis, whereas look vectors use its local axis.
So you can point it in a direction and it will move in that direction, which is super cool and something
that would be quite difficult to do otherwise.
So I'll keyframe might not be the most overly interesting topic.
Um, I can think of quite a few at probably a more interesting.
It does have lots of applications.
So um, and especially I think later on, maybe when maybe even after you finish this course, um,
and you learn making some more advanced scripts, you'll understand the more the application C-frame
has.
And yeah, I think it's more so of a, the more advanced scripting you do, the more you'll encounter
keyframe.
Really.
So it is really cool.
Um, so hopefully you enjoyed learning about it.
And in the next lecture, we're going to have a look at one of the applications of this to do of the
camera.
So you can't position cameras normally by using vector threes, but you can position them using um keyframe.
So we're going to take a look at that in the next lecture.
So join me in the next lecture.
We have a look at the camera.